<?php
/**
 * @分销
 * @功能说明: 考拉商品下单接口
 * @更新说明: 暂无更新
 * @文件名: KlorderController.class.php
 * @编码: UTF-8
 * @创建时间: 2016-03-25 15:37
 * @创建人: WENCUN
 */

namespace dg\Controller;
use Think\Controller;

class KlorderController extends Controller
{
    /*
     * 考拉商品下单
     * @date: 2016-03-25 15:38:00
     * @editor: WENCUN
     * */
    public function bookOrder(){
        //@todo sky 验证参数暂时未做判断
        if(!I('get.order')){
            jsonRespons(0,'参数有误');
        }
        //参数: 订单ID 考拉商品ID 考拉商品SkuID 商品购买数量
        $goodsId = M('OrderCollect')->field(array('goods_id','pay_nums'))->where(array('order_id'=>I('get.order')))->select();
        if(!$goodsId){
            jsonRespons(0,'参数有误');
        }
        foreach($goodsId as $key=>$value){
            $klgoods = M('GoodsKlwait')->where(array('goods_id'=>$value['goods_id']))->find();
            if($klgoods){
                $goodsMsg[$key] = array(
                    'goods_id' => $value['goods_id'],
                    'sku_id' => $klgoods['sku_id'],
                    'kl_goods_id' => $klgoods['kl_goods_id'],
                    'pay_nums' => intval($value['pay_nums']),
                    'storage' => $klgoods['storage']
                );
            }
        }
        if(empty($goodsMsg)){
            jsonRespons(0,'商品有误');
        }
        //商品数据重组
        foreach($goodsMsg as $v){
            $newGoods[$v['storage']]['goods'][] = $v;
            $newGoods[$v['storage']]['orderId'] = I('get.order').randstr().rand(0,10000);
        }
        //商品下单数据
        foreach($newGoods as $k=>$v){
            $info[$k] = array(
                'orderId' => $v['orderId']
            );
            foreach($v['goods'] as $key=>$value){
                $info[$k]['goods_id'][$key] = $value['goods_id'];
            }
            $param[] = self::klOrder($v['orderId'],$v['goods']);
        }
        //下单
        foreach($param as $key=>$value){
            //下单确认
            $orderConfirm[$key] = curl('http://223.252.220.85/api/orderConfirm',$value);
            $confirmMsg[$key] = json_decode($orderConfirm[$key],true);
            if($confirmMsg[$key]['recCode'] != 200){
                jsonRespons(0,$confirmMsg[$key]['recMeg']);
            }
            //下单支付
            $order[$key] = curl('http://223.252.220.85/api/bookorder',$value);
            $orderMsg[$key] = json_decode($order[$key],1);
            if($orderMsg[$key]['recCode'] != 200){
                jsonRespons(0,$orderMsg[$key]['recMeg']);
            }
        }
        dataFormat($info,1,'购买成功');
    }


    /*
     * 考拉代下单请求
     * @date: 2016-03-25 15:51:00
     * @editor: WENCUN
     * */
    static function klOrder($orderId,$goods){
        $secretKey = 'bfa9602ec39e1d18ef434cbdfe9ff0ad25038ad94fca338314ed04d4b4688f58';
        $appKey = '417635e229aa376bdff8d17c50fd15171f034584e550986eb1ededc930f3633a';
        $httpHeader = array(
            'timestamp' => date('Y-m-d H:i:s'),//时间戳
            'app_key' => $appKey,
            'v' => '1.0',//API版本
            'sign' => strtoupper(md5($secretKey.'app_key'.$appKey.'timestamp'.date('Y-m-d H:i:s').'v1.0'.$secretKey)),//签名
            'sign_method' => 'md5',//签名方法
        );
        if($orderId){
            $httpHeader['thirdPartOrderId'] = $orderId;//分销商订单ID
        }
        if($goods){
            //orderItemList 订单项
            foreach($goods as $key=>$value){
                $orderItemList[$key] = array(
                    'goodsId' => $value['kl_goods_id'], //商品id
                    'skuId' => $value['sku_id'], //商品skuId
                    'buyAmount' => $value['pay_nums'] //购买数量
                );
            }
            //userInfo 用户信息
            $userInfo = array(
                'accountId' => '12', //用户id
                'name' => '李剑锋', //用户姓名
                'mobile' => '18681885454', //手机号码
                'email' => '2752734232@qq.com', //邮箱
                'provinceName' => '浙江省', //省份名称
                'provinceCode' => '', //省份代码(非必须)
                'cityName' => '杭州市', //城市名称
                'cityCode' => '', //城市代码(非必须)
                'districtName' => '拱墅区', //县（区）名称
                'districtCode' => '', //县（区）代码(非必须)
                'address' => '祥园路', //街道地址
                'postCode' => '', //邮编(非必须)
                'phoneNum' => '', //固定电话(非必须)
                'phoneAreaNum' => '', //区号(非必须)
                'phoneExtNum' => '', //分机号(非必须)
                'identityName' => '李剑锋', //证件姓名(实名信息)
                'identityId' => '35058319891019003X', //证件号码(实名信息)
            );

            $httpHeader['source'] = '1100';//渠道商ID
            $httpHeader['orderItemList'] = json_encode(array('orderItemList'=>$orderItemList));//订单项
            $httpHeader['userInfo'] = json_encode(array('userInfo'=>$userInfo)); //用户信息
        }else{
            $httpHeader['channelId'] = '1100';//渠道商ID
        }
        return $httpHeader;
    }

    /*
     * 考拉商品订单状态查询
     * @date: 2016-03-30 10:37:00
     * @editor: WENCUN
     * */
    public function queryOrder(){
        if(I('get.order')){
            $param = self::klOrder(I('get.order'));
            $orderJson = curl('http://223.252.220.85/api/queryOrderStatus',$param);
            $orderMsg = json_decode($orderJson,true);
            if($orderMsg['recCode'] == 200){
                foreach($orderMsg['result'][0]['skuList'] as $key=>$value){
                    $skuList[$key]['sku_id'] = $value['skuid'];
                    $skuList[$key]['num'] = $value['buyCnt'];
                }
                $info = array(
                    'status' => $orderMsg['result'][0]['status'],
                    'express_num' => $orderMsg['result'][0]['deliverNo'],
                    'express_name' => $orderMsg['result'][0]['deliverName'],
                    'skuList' => $skuList
                );
                dataFormat($info,1,'success');
            }else{
                jsonRespons(0,$orderMsg['recMeg']);
            }
        }else{
            jsonRespons(0,'参数有误');
        }
    }
}